/**
 * @Title: AuthMechanismActivity.java
 * @Copyright: XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * @Description: TODO<请描述此文件是做什么的>
 * @author: MeiYi
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */

package artid.com.arttogo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.AtMeAdapter;
import artid.com.arttogo.adapter.ListAdapter;
import artid.com.arttogo.bean.CommentList;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;

/**
 * TODO<请描述这个类是干什么的>
 * At我的
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class ATmeActivity extends BaseActivity {
    private Context mContext;
    private ListView listview;
    private AtMeAdapter adapter;
    private ImageView back;
    private TextView title;
    private ListAdapter typeAdapter;
    PullToRefreshLayout mPullLayout;
    int page = 1, pagesize = 20;
    View mViewBlank;

    /*
     * (non-Javadoc)
     *
     * @see com.sigmatrix.artworkart.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atme);
        mViewBlank = (View) findViewById(R.id.atme_blank);
        mContext = ATmeActivity.this;
        listview = (ListView) findViewById(R.id.listivew);
        back = (ImageView) findViewById(R.id.back_btn);
        title = (TextView) findViewById(R.id.tittle_text);
        title.setText("AT我的");
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        loading = (RelativeLayout) findViewById(R.id.rel_progressbar);
        mPullLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        mPullLayout.setVisibility(View.INVISIBLE);
        mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                page = 1;
                requestGradeList();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                // pullToRefreshLayout.setBottomDismiss();
                page++;
                requestGradeList();
            }
        });

//		AppUrl.newsList(AuthNewsletterActivity.this);
//		requestGradeList();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        requestGradeList();
    }

    private void requestGradeList() {
        VolleyBasic.getInstance(ATmeActivity.this).post(Cache.commentList,
                VolleyMap.getInstance().commentList(String.valueOf(page), String.valueOf(pagesize)), CommentList.class,
                new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
                        List<CommentList> list = (List<CommentList>) vd.getList();
                        if (list.isEmpty() && page == 1) {
                            mViewBlank.setVisibility(View.VISIBLE);
                            return;
                        }
                        mViewBlank.setVisibility(View.GONE);
                        mPullLayout.setVisibility(View.VISIBLE);
                        if (page != 1) {
                            adapter.addList(list);
                            setItemClick();
                            return;
                        }
                        adapter = new AtMeAdapter(mContext, list);
                        listview.setAdapter(adapter);
                        setItemClick();

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub
                        mPullLayout.loadmoreFinish(mPullLayout.FAIL);
                    }
                });
    }

    void setItemClick() {
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(mContext, CollectionDetailPagerActivity.class);
                CommentList item = adapter.list.get(arg2);
                intent.putExtra("messid", item.getId());
                intent.putExtra("id", item.getArt_id());
                intent.putExtra("titleName", item.getTitle());
                startActivity(intent);
            }

        });
    }


}
